package cn.edu.gduf.luowowo.mgrsite.job;

import cn.edu.gduf.luowowo.article.domain.StrategyDetail;
import cn.edu.gduf.luowowo.article.service.IStrategyDetailService;
import cn.edu.gduf.luowowo.ask.domain.Question;
import cn.edu.gduf.luowowo.ask.service.IQuestionService;
import cn.edu.gduf.luowowo.cache.domain.QuestionVo;
import cn.edu.gduf.luowowo.cache.domain.StrategyStatisVO;
import cn.edu.gduf.luowowo.cache.service.IQuestionVoService;
import cn.edu.gduf.luowowo.cache.service.IStrategyStatisVoRedisService;
import cn.edu.gduf.luowowo.common.util.BeanUtil;
import cn.edu.gduf.luowowo.common.util.RedisKeys;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisDataPersistenceJob {
    @Reference
    private IStrategyStatisVoRedisService strategyStatisVoRedisService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IQuestionService questionService;
    @Reference
    private IQuestionVoService questionVoService;

    /**
     * 对redis中数据持久化操作
     * Seconds Minutes Hours DayofMonth Month DayofWeek Year
     * 秒        分      时      号        月      星期    年
     * Seconds Minutes Hours DayofMonth Month DayofWeek
     * 秒        分      时      号        月      星期
     * <p>
     * 0/10      *      *       *         *        ?
     * 每隔10秒执行一次
     */
   /* @Scheduled(cron = "0/10 * * * * ? ")
    public void doPersistence() {
        //拿到redis中的所有数据统计对象，vo
        List<StrategyStatisVO> list = strategyStatisVoRedisService.getStrategyStatisVOs(RedisKeys.STARTEGY_STATIS_VO.join("*"));
        //遍历，将每个vo变成一个个strategydetail对象
        StrategyDetail detail = null;
        for (StrategyStatisVO vo : list) {
            detail = new StrategyDetail();
            BeanUtil.copyProperties(detail, vo);
            detail.setId(vo.getStrategyId());
            //更新mysql中的数据
            strategyDetailService.updateStrategyDetailForStatis(detail);
        }
    }*/
    @Scheduled(cron = "0/10 * * * * ? ")
    public void askPersistence() {
        //拿到redis中的所有数据统计对象，vo
        List<QuestionVo> list = questionVoService.getQuestionStatisVos(RedisKeys.QUESTION_STATIS_VO.join("*"));
        //遍历，将每个vo变成一个个strategydetail对象
        if (list != null && list.size() > 0) {
            for (QuestionVo vo : list) {
                Question question = new Question();
                BeanUtil.copyProperties(question, vo);
                question.setId(vo.getQuestionId());
                //更新mysql中的数据
                questionService.updateQuestionForStatis(question);
            }
        }
        System.out.println("执行问答对象持久化");
    }
}
